package com.bit.forum.services.impl;

import com.bit.forum.common.AppResult;
import com.bit.forum.common.ResultCode;
import com.bit.forum.dao.BoardMapper;
import com.bit.forum.exception.ApplicationException;
import com.bit.forum.model.Board;
import com.bit.forum.services.IBoardService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
@Slf4j
public class BoardServiceImpl implements IBoardService {

    @Resource
    private BoardMapper boardMapper;

    @Override
    public List<Board> selectByNum(Integer num) {
      //非空校验
      if (num <=0){
          log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
          throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
      }
      //调用数据库中的数据
        List<Board> result = boardMapper.selectByNum(num);
        return result;
    }

    @Override
    public Board selectById(Long id) {
        if (id == null || id <= 0){
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_ARTICLE_COUNT));

        }
        //调用dao查询数据
        Board board = boardMapper.selectByPrimaryKey(id);
        return board;
    }

    @Override
    public void addOneArticleCountById(Long id) {
        if (id == null || id <= 0){
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_ARTICLE_COUNT));

        }
        //查询对应的板块-贴子数多少
        Board board = boardMapper.selectByPrimaryKey(id);
        if (board == null){
            //没有当前板块
            log.warn(ResultCode.ERROR_IS_NUL.toString() + ",board id = "+ id );
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_IS_NUL));
        }
        //更新帖子数量
        //重新创建一个用来更新的对象
        Board updateBoard = new Board();
        updateBoard.setId(board.getId());  //板块id
        updateBoard.setArticleCount(board.getArticleCount()+1);  //板块数量＋1
        //执行更新
        int row = boardMapper.updateByPrimaryKeySelective(updateBoard);
        //判断受影响的行数
        if (row != 1){
            log.warn(ResultCode.FAILED.toString() + ",受影响的行数不等于1");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }
    }

    @Override
    public void subOneArticleCountById(Long id) {
        //判断参数
        if (id == null || id <= 0){
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }
        //查询板块详情
        Board board = boardMapper.selectByPrimaryKey(id);
        if (board == null){
            log.warn(ResultCode.FAILED_BOARD_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }
        //构造更新对象
        Board updateboard = new Board();
        updateboard.setId(board.getId());
        updateboard.setArticleCount(board.getArticleCount()-1);
        //判断-1后是否小于0
        if (updateboard.getArticleCount() < 0){
            //小于0,设置为0
            updateboard.setArticleCount(0);
        }
        int row = boardMapper.updateByPrimaryKeySelective(updateboard);
        if (row != 1){
            log.warn(ResultCode.FAILED.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }
    }
}
